﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices;

    public class StaffClass
    {
        private readonly Chain.IDAL.StaffClass dal = new Chain.IDAL.StaffClass();

        public int Add(Chain.Model.StaffClass model)
        {
            if (this.Exists(model.ClassID, model.ClassName, model.ClassShopID))
            {
                return -1;
            }
            return this.dal.Add(model);
        }

        public List<Chain.Model.StaffClass> DataTableToList(DataTable dt)
        {
            List<Chain.Model.StaffClass> list = new List<Chain.Model.StaffClass>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.StaffClass item = new Chain.Model.StaffClass();
                    if ((dt.Rows[i]["ClassID"] != null) && (dt.Rows[i]["ClassID"].ToString() != ""))
                    {
                        item.ClassID = int.Parse(dt.Rows[i]["ClassID"].ToString());
                    }
                    if ((dt.Rows[i]["ClassName"] != null) && (dt.Rows[i]["ClassName"].ToString() != ""))
                    {
                        item.ClassName = dt.Rows[i]["ClassName"].ToString();
                    }
                    if ((dt.Rows[i]["ClassType"] != null) && (dt.Rows[i]["ClassType"].ToString() != ""))
                    {
                        if ((dt.Rows[i]["ClassType"].ToString() == "1") || (dt.Rows[i]["ClassType"].ToString().ToLower() == "true"))
                        {
                            item.ClassType = true;
                        }
                        else
                        {
                            item.ClassType = false;
                        }
                    }
                    if ((dt.Rows[i]["ClassPercent"] != null) && (dt.Rows[i]["ClassPercent"].ToString() != ""))
                    {
                        item.ClassPercent = decimal.Parse(dt.Rows[i]["ClassPercent"].ToString());
                    }
                    if ((dt.Rows[i]["ClassShopID"] != null) && (dt.Rows[i]["ClassShopID"].ToString() != ""))
                    {
                        item.ClassShopID = int.Parse(dt.Rows[i]["ClassShopID"].ToString());
                    }
                    if ((dt.Rows[i]["ClassRemark"] != null) && (dt.Rows[i]["ClassRemark"].ToString() != ""))
                    {
                        item.ClassRemark = dt.Rows[i]["ClassRemark"].ToString();
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int ClassID)
        {
            return this.dal.Delete(ClassID);
        }

        public bool DeleteList(string ClassIDlist)
        {
            return this.dal.DeleteList(ClassIDlist);
        }

        public bool Exists(int ClassID)
        {
            return this.dal.Exists(ClassID);
        }

        public bool Exists(int intClassID, string strClassName, int ShopID)
        {
            return this.dal.Exists(intClassID, strClassName, ShopID);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            return this.dal.GetListSP(PageSize, PageIndex, out resCount, strWhere);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.StaffClass GetModel(int ClassID)
        {
            return this.dal.GetModel(ClassID);
        }

        public List<Chain.Model.StaffClass> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public int Update(Chain.Model.StaffClass model)
        {
            if (this.Exists(model.ClassID, model.ClassName, model.ClassShopID))
            {
                return -1;
            }
            return this.dal.Update(model);
        }
    }
}

